﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class TicketCommentBus
    {
        private TicketCommentDao dao;
        public TicketCommentBus()
        {
            dao = new TicketCommentDao();
        }

        public bool insert(TicketComment ticketComment) {
            return dao.insert(ticketComment);
        }

        public bool update(TicketComment ticketComment) {
            return dao.update(ticketComment);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public TicketComment get(int id) {
            DataTable dt = dao.get(id);
            TicketComment ticketComment = new TicketComment();

            if (dt.Rows.Count > 0)
            {
                ticketComment.Id = Convert.ToInt32(dt.Rows[0]["TCM_ID"]);
                ticketComment.TicketId = Convert.ToInt32(dt.Rows[0]["TKT_ID"]);
                ticketComment.Content = dt.Rows[0]["TCM_CONTENT"].ToString();
                ticketComment.TimeStamp = Convert.ToDateTime(dt.Rows[0]["TCM_TIMESTAMP"]);
                ticketComment.ParentId = Convert.ToInt32(dt.Rows[0]["PARENT_ID"]);
                ticketComment.ParentType = dt.Rows[0]["PARENT_TYPE"].ToString();
                ticketComment.Attachment1 = dt.Rows[0]["TCM_ATTACHMENT1"].ToString();
                ticketComment.Attachment2 = dt.Rows[0]["TCM_ATTACHMENT2"].ToString();
                ticketComment.Attachment3 = dt.Rows[0]["TCM_ATTACHMENT3"].ToString();
            }
            return ticketComment;
        }

        public List<TicketComment> getAll(int ticketId) {
            DataTable dt = dao.getAll(ticketId);
            List<TicketComment> list = new List<TicketComment>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TicketComment ticketComment = new TicketComment();
                    ticketComment.Id = Convert.ToInt32(dr["TCM_ID"]);
                    ticketComment.TicketId = Convert.ToInt32(dr["TKT_ID"]);
                    ticketComment.Content = dr["TCM_CONTENT"].ToString();
                    ticketComment.TimeStamp = Convert.ToDateTime(dr["TCM_TIMESTAMP"]);
                    ticketComment.ParentId = Convert.ToInt32(dr["PARENT_ID"]);
                    ticketComment.ParentType = dr["PARENT_TYPE"].ToString();
                    ticketComment.Attachment1 = dr["TCM_ATTACHMENT1"].ToString();
                    ticketComment.Attachment2 = dr["TCM_ATTACHMENT2"].ToString();
                    ticketComment.Attachment3 = dr["TCM_ATTACHMENT3"].ToString();

                    list.Add(ticketComment);
                }

            }
            return list;
        }

        public bool deleteByTicket(int ticketId)
        {
            return dao.deleteByTicket(ticketId);
        }
    }
}
